import 'dart:io';

import 'package:flutter/material.dart';
import 'package:image_cropper/image_cropper.dart';
import 'package:image_picker/image_picker.dart';

import 'const.dart';

class MyImagePicker extends StatefulWidget {
  final String type;

  MyImagePicker({String type}) : this.type = type;

  @override
  _MyImagePickerState createState() => _MyImagePickerState(type: type);
}

class _MyImagePickerState extends State<MyImagePicker> {
  String type;
  var _imgPath;

  _MyImagePickerState({String type}) : this.type = type;

  /*拍照*/
  _takePhoto() async {
    File image = await ImagePicker.pickImage(source: ImageSource.camera);
    setState(() {
      if (image == null) {
        Navigator.of(context).pop();
        return;
      }
      _imgPath = image;
    });
  }

/*相册*/
  _openGallery() async {
    File image = await ImagePicker.pickImage(source: ImageSource.gallery);
    setState(() {
      if (image == null) {
        Navigator.of(context).pop();
        return;
      }
      _imgPath = image;
    });
  }

  /*图片控件*/
  _ImageView(imgPath) {
    if (imgPath == null) {
      return Center(
        child: Icon(
          Icons.image,
          color: Colors.grey,
          size: 200,
        ),
      );
    } else {
      return Image.file(
        imgPath,
        width: MediaQuery.of(context).size.width,
        height: MediaQuery.of(context).size.width,
      );
    }
  }

  @override
  void initState() {
    super.initState();
    if (type == Const.Camera) {
      _takePhoto();
    } else if (type == Const.Picture) {
      _openGallery();
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Const.black,
      appBar: AppBar(
        title: Text(
          '裁剪图像',
        ),
        actions: <Widget>[
          IconButton(
            onPressed: () {
              _cropImage();
            },
            icon: Icon(
              Icons.content_cut,
              color: Const.gold,
            ),
          )
        ],
      ),
      body: Column(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: <Widget>[
          Padding(
            padding: const EdgeInsets.all(16),
            child: _ImageView(_imgPath),
          ),
          MaterialButton(
            height: 68,
            minWidth: MediaQuery.of(context).size.width,
            elevation: 0,
            onPressed: () {
              Navigator.pop(context, _imgPath);
            },
            child: Text(
              "确    定",
              style: TextStyle(color: Colors.white, fontSize: 20),
            ),
            color: Const.gold,
            shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.only(
                    topLeft: Radius.circular(30),
                    bottomRight: Radius.circular(30))),
          ),
        ],
      ),
    );
  }

  _cropImage() async {
//    File croppedFile = await ImageCropper.cropImage(
//      sourcePath: _imgPath.path,
//    );
//    if (croppedFile != null) {
//      File image = croppedFile;
//      setState(() {
//        _imgPath = image;
//      });
//    }

    File croppedFile = await ImageCropper.cropImage(
        sourcePath: _imgPath.path,
        aspectRatioPresets: [
          CropAspectRatioPreset.square,
          CropAspectRatioPreset.ratio3x2,
          CropAspectRatioPreset.original,
          CropAspectRatioPreset.ratio4x3,
          CropAspectRatioPreset.ratio16x9
        ],
        androidUiSettings: AndroidUiSettings(
            toolbarTitle: 'Cropper',
            toolbarColor: Colors.deepOrange,
            toolbarWidgetColor: Colors.white,
            initAspectRatio: CropAspectRatioPreset.original,
            lockAspectRatio: false),
        iosUiSettings: IOSUiSettings(
          minimumAspectRatio: 1.0,
        ));
    if (croppedFile != null) {
      File image = croppedFile;
      setState(() {
        _imgPath = image;
      });
    }
  }

//  void _clearImage() {
//    imageFile = null;
//    setState(() {
//      state = AppState.free;
//    });
//  }
}
